package com.bwie.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云短信的简单使用
 */
public class AliYunSmsUtil {

    public static String sendSms(String code, String phoneNumber) {
        String host = "https://wwsms.market.alicloudapi.com";
        String path = "/send_sms";
        String method = "POST";
        String appcode = "6bc09d0e7e694a51ba928bd702239538";  //晓朱莉的appcode
        Map<String, String> headers = new HashMap<>();

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);

        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("content", "code:" + code);
        bodys.put("template_id", "CST_11253");   //注意，模板CST_11253 仅作调试使用，下发短信不稳定，请联系客服报备自己的专属签名模板，以保障业务稳定使用
        bodys.put("phone_number", phoneNumber);

        String result = "";
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

            // 获取response的body
            String entityStr = EntityUtils.toString(response.getEntity());
            System.out.println(entityStr);
            JSONObject jsonObject = JSONObject.parseObject(entityStr);
            result = (String) jsonObject.get("status");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}
